﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using SimpleNoteLib.Annotations;
using SimpleNoteLib.Extensions;
using SimpleNoteLib.Json;

namespace SimpleNoteLib
{
    /// <summary>
    /// Simple Note without the content
    /// </summary>
    [DataContract]
    public class NoteLite : INotifyPropertyChanged
    {
        private DateTime? modifyDate;
        private ICollection<string> tags;
        private bool isDeleted;
        private DateTime? createdDate;
        private ICollection<string> systemTags;

        /// <summary>
        /// Modify date
        /// </summary>
        [DataMember]
        public DateTime? ModifyDate
        {
            get { return modifyDate; }
            set
            {
                if (value.Equals(modifyDate)) return;
                modifyDate = value;
                OnPropertyChanged("ModifyDate");
            }
        }

        /// <summary>
        /// Tags
        /// </summary>
        [DataMember]
        public ICollection<string> Tags
        {
            get { return tags; }
            set
            {
                if (Equals(value, tags)) return;
                tags = value;
                OnPropertyChanged("Tags");
            }
        }

        /// <summary>
        /// Indicates if note is in trash
        /// </summary>
        [DataMember]
        public bool IsDeleted
        {
            get { return isDeleted; }
            set
            {
                if (value.Equals(isDeleted)) return;
                isDeleted = value;
                OnPropertyChanged("IsDeleted");
            }
        }

        /// <summary>
        /// Creation date
        /// </summary>
        [DataMember]
        public DateTime? CreatedDate
        {
            get { return createdDate; }
            set
            {
                if (value.Equals(createdDate)) return;
                createdDate = value;
                OnPropertyChanged("CreatedDate");
            }
        }

        /// <summary>
        /// System tags
        /// </summary>
        [DataMember]
        public ICollection<string> SystemTags
        {
            get { return systemTags; }
            set
            {
                if (Equals(value, systemTags)) return;
                systemTags = value;
                OnPropertyChanged("SystemTags");
            }
        }

        /// <summary>
        /// Note version
        /// </summary>
        [DataMember]
        public int Version { get; protected set; }

        /// <summary>
        /// Note synchronization number
        /// </summary>
        [DataMember]
        public int SyncNumber { get; protected set; }

        /// <summary>
        /// Note key
        /// </summary>
        [DataMember]
        public string Key { get; protected set; }

        /// <summary>
        /// First version of note
        /// </summary>
        [DataMember]
        public int MinVersion { get; protected set; }

        /// <summary>
        /// Creates new Note
        /// </summary>
        internal NoteLite()
        {
        }

        internal static NoteLite FromJson(NoteLiteJson noteJson)
        {
            NoteLite result = new NoteLite()
            {
                CreatedDate = DateTimeExtensions.FromUnixTimeInString(noteJson.createdate),
                IsDeleted = noteJson.deleted == 1,
                Key = noteJson.key,
                MinVersion = noteJson.minversion,
                ModifyDate = DateTimeExtensions.FromUnixTimeInString(noteJson.modifydate),
                SyncNumber = noteJson.syncnum,
                SystemTags = new List<string>(noteJson.systemtags),
                Tags = new List<string>(noteJson.tags),
                Version = noteJson.version
            };
            return result;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}